from vector import vector
from rendering import *
from enemySpawner import *
import tower
col = []
def getByName(string,l):
    for i in range(0,len(l)):
        if str(l[i]) == string:
            return l[i+1]
class load_map2:
    def __init__(self,path,surface):
        self.surface = surface
        f = open(path,"r")
        fString = f.read()
        f.close()
        data = fString.split(";")
        entityList = eval(data[0])
        col = eval(data[1])
        self.map = i_object(data[2],self.surface)
        for entity in entityList:
            if entity[1] == "Base.txt":
                tower.Base(self.surface,vector(entity[0][0],entity[0][1]))
    def changeMap(self,path):
        f = open(path,"r")
        fString = f.read()
        f.close()
        data = fString.split(";")
        entityList = eval(data[0])
        col = eval(data[1])
        self.map = i_object(data[2])
    def redraw(self):
        self.map.draw(vector(0,0))
class load_map:
    def __init__(self,path,surface):
        self.surface = surface
        f = open(path,"r")
        fString = f.read()
        f.close()
        fString = fString.split(";")
        entityList = eval(fString[0])
        textures = eval(fString[1])
        self.mapa = eval(fString[2])
        self.images = []
        for entity in entityList:
            if entity[1] == "Base.txt":
                tower.Base(self.surface,vector(entity[0][0],entity[0][1]))
        for i in textures:
            self.images.append(i)
            self.images.append(i_object("./Images/"+i,surface))
        for x in range(0,80):
            for y in range(0,60):
                if self.mapa[x][y][0] != "v":
                    if self.mapa[x][y][2] == True:
                        col.append(self.mapa[x][y][0])
    def changeMap(self,path):
        self.mapa = []
        f = open(path,"r")
        fString = f.read()
        f.close()
        fString = fString.split(";")
        textures = eval(fString[0])
        self.map = eval(fString[1])
        self.images = []
        for i in textures:
            self.images.append(i)
            self.images.append(i_object("./Images/"+i,surface))
        for x in range(0,80):
            for y in range(0,60):
                if self.mapa[x][y][0] != "v":
                    if self.mapa[x][y][2] == True:
                        col.append(i[0])
    def returnEntityList(self):
        return self.entity
    def redraw(self):
        for x in range(0,80):
            for y in range(0,60):
                if self.mapa[x][y][0] != "v":
                    pos = self.mapa[x][y][0].split("x")
                    getByName(self.mapa[x][y][1],self.images).draw(vector(int(pos[0])*10,int(pos[1])*10))

